package com.zhenti.z1;

import java.util.Arrays;
import java.util.HashMap;

/*
* 现有两个字符串s1，s2，
*       判断s2的排序之一是否是s1的子字符串
*       是则返回true ，不是则返回false
*       */
public class Zijiezhenti {
    public static void main(String[] args) {
        String s1 = "aagfbddddbaacs";
        String s2 = "aadbc";
        System.out.println(Solution2(s1, s2));
    }
    public Boolean Solution(String s1,String s2){
        if (s1 == null ||s1.length() < s2.length())
            return false;

        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s2.length(); i++) {
            Character c = s2.charAt(i);
            if (map.containsKey(c)){
                map.put(c,1);
            }else {
                map.put(c,map.get(c)+1);
            }
        }
        int num = s2.length();
        for (int i = 0; i < s1.length(); i++) {
            Character c = s1.charAt(i);
            if (map.containsKey(c)){

            }
        }


        return false;
    }

    public static boolean Solution2(String s1,String s2){
        if (s1.length() < s2.length())
            return false;
        char[] str2 = s2.toCharArray();
        Arrays.sort(str2);
        s2 = String.valueOf(str2);
        for (int i = 0; i <= s1.length() - s2.length(); i++) {
            String s = s1.substring(i, i + s2.length());
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            String valueOf = String.valueOf(chars);
            if (valueOf.equals(s2))
                return true;
        }

        return false;
    }
}
